﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib;

namespace ReBiaoDTU
{
    public class ReBiaoData
    {
        public byte Addr;
        public ulong SheBeiID;
        public ushort ZhiZaoShanID;
        public byte FangWenXuHao;
        public byte Status;
        public double ReLiang;
        public double LeiJiLiuLiang;
        public double JinShuiWenDu;
        public double HuiShuiWenDu;
        public double ShunShiLiuLiang;
        public double GongLv;
        public DateTime dt;
        public ReBiaoData()
        {

        }
        public static ReBiaoData MakeReBiaoData(byte[] data, int offset)
        {
            byte[] b = new byte[61];
            for (int i = 0; i < 61; i++)
            {
                b[i] = data[offset + i];
            }
            return MakeReBiaoData(b);
        }
        public static ReBiaoData MakeReBiaoData(byte[] data)
        {
            ReBiaoData ret=null;
            if (data.Length == 61)
            {
                if (GetVerify_byteSum(data, 4, 55) == data[59])
                {
                    ret = new ReBiaoData();
                    ret.Addr = data[5];
                    ret.SheBeiID = BytesOP.MakeInt(data[10], data[9], data[8], data[7]);
                    ret.ZhiZaoShanID = BytesOP.MakeShort(data[12], data[11]);
                    ret.FangWenXuHao = data[15];
                    ret.Status = data[16];
                    ret.ReLiang = BytesOP.uint2BCD(BytesOP.MakeInt(data[24], data[23], data[22], data[21]));
                    ret.LeiJiLiuLiang = BytesOP.uint2BCD(BytesOP.MakeInt(data[30], data[29], data[28], data[27]))/1000;
                    ret.JinShuiWenDu = BytesOP.uint2BCD(BytesOP.MakeInt(0,data[35], data[34], data[33]))/100;
                    ret.HuiShuiWenDu = BytesOP.uint2BCD(BytesOP.MakeInt(0,data[40], data[39], data[38]))/100;
                    ret.ShunShiLiuLiang = BytesOP.uint2BCD(BytesOP.MakeInt(data[46], data[45], data[44], data[43]))/1000;
                    ret.GongLv = BytesOP.uint2BCD(BytesOP.MakeInt(data[52], data[51], data[50], data[49]))/1000;
                    ret.dt = new DateTime(2000+(BytesOP.GetHighNibble(data[58])<<3)+(BytesOP.GetHighNibble(data[57])>>1),
                                          BytesOP.GetLowNibble(data[58]),
                                          (byte)(data[57]&0x1f),
                                          data[56],
                                          data[55],0);
                }
            }
            return ret;
        }
        public static byte GetVerify_byteSum(byte[] pBuff, int start, int count)
        {
            byte sum = 0;
            for (int i = 0; i < count; i++)
                sum = (byte)(sum + pBuff[i + start]);
            return sum;
        }
    }

}
